var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('admin/user/authlist')
});

router.get('/api/authlist',async (req,res)=>{
    let page = parseInt(req.query.page);  //第几页
    let limitNum = parseInt(req.query.limit); //限制多少条
    let sqlStr = "select * from auth limit ?,?";
    let arr = [(page-1)*limitNum,limitNum]
    let result = await sqlQuery(sqlStr,arr);
     //获取总数
    let sqlStr1 = "select count(id) as number from auth"
    let result1 = await sqlQuery(sqlStr1)
    let count = result1[0].number
    let options ={
        "code": 0,
        "msg": "",
        "count": count,
        "data": Array.from(result)
      }
      res.json(options)
})


//获取所有的权限列表
router.get('/api/authlistAll',async (req,res)=>{
    let sqlStr = "select * from auth";
    let result = await sqlQuery(sqlStr);
    let options ={
        "code": 0,
        "data": Array.from(result)
      }
      res.json(options)
})



//添加权限
router.get('/addauth',(req,res)=>{
    res.render('admin/user/addauth')
})


//获取前端提交的数据
router.post('/addauth',async(req,res)=>{
    console.log(req.body)
    let authname = req.body.authname
    let authurl = req.body.authurl
    let brief = req.body.brief
    let sqlStr = "insert into auth (authname,authurl,brief) values(?,?,?)"
    let arr = [authname,authurl,brief]
    await sqlQuery(sqlStr,arr)
    res.json({
        state:"ok",
        content:"添加权限成功成功"
      })
})



//删除权限
router.get('/delauth',async(req,res)=>{
    let userid = req.query.userid
    let sqlStr = "delete from auth where id = ?";
    let result = await sqlQuery(sqlStr,[userid])
    res.json({
      state:"ok",
      content:Array.from(result)
    })
  })




  //编辑权限
  router.get('/editauth',async function (req, res, next) {
    //获取用户id
    let userid = req.query.id;
    let sqlStr = "select * from auth where id = ?";
    let result = await sqlQuery(sqlStr,[userid])
    let auth = result[0]
    let options = {auth};
    res.render('admin/user/editauth',options);

  });


  //修改用户信息，post提交
  router.post('/editauth',async(req,res)=>{
    let authid = req.body.authid
    let authname = req.body.authname
    let brief = req.body.brief
    let authurl = req.body.authurl

    //更新数据
    let sqlStr = "update auth set authname=?, authurl=?, brief=? where id=? ";
    let arr = [authname,authurl,brief,authid]
    await sqlQuery(sqlStr,arr);
        res.json({
          state:"ok",
          content:"权限编辑成功"
        })
  })




//验证添加权限时权限名称和权限地址是否已存在
router.post('/testing',async(req,res)=>{
  //判断用户名是否存在
  let authname = req.body.authname
  let authurl = req.body.authurl
  let sqlStr1 = "select * from auth where authname =? and authurl =?"
  let result1 = await sqlQuery(sqlStr1,[authname,authurl])
  if(result1.length == 0){
      res.json({
          state:"ok",
          content:"没有匹配到可以添加"
      })
  }else{
      res.json({
          state:"fail",
          content:"权限名称或者权限地址已存在"
      })
  }
})













module.exports = router;